package tests;

import static org.junit.Assert.*;

import java.util.List;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import classes.Market;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

import dao.DAOFactory;
import classes.*;

public class Testni {
	private static Tip p;
	private static WebResource service;
	
	
	@BeforeClass
	public static void fixtureSetUp() throws Exception{
		DAOFactory.connect();
	}
	
	@AfterClass
	public static void fixtureTearDown() throws Exception{
		DAOFactory.disconnect();
	}
	
	@Before
	public void setUp() throws Exception{
		ClientConfig config = new DefaultClientConfig();
	    Client client = Client.create(config);
	    service = client.resource(UriBuilder.fromUri("http://localhost:8080/eKupovina").build());
	}
	@Test 
	public void dajSveTipove() throws Exception{
		try{
			List<Tip> marketi= service.path("webservices").path("tipovi").path("svi").accept(MediaType.APPLICATION_XML).get(new  GenericType<List<Tip>>() {});
			assertNotNull(marketi.size());
		}catch(Exception ex){
			System.out.println(ex.getMessage());
		}
		}
	@Test 
	public void dodajTip() throws Exception{
		try{
			Tip t=new Tip();
			t.setNaziv("noviTip");
			String user="bingo";
			String pass="bingo";
			String s= service.path("webservices").path("tipovi").path("tip").path("novi").header("username", user).header("password", pass).type(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML).put(String.class, t);
			Tip t2=DAOFactory.getTipDAO().selectTip(t.getIdTip());
			assertEquals(t.getNaziv(), t2.getNaziv());
		}catch(Exception ex){
			System.out.println(ex.getMessage());
		}
	}
}

